
#include "FileTask.h"


FileTask::FileTask(int fd, QDReqParams *info){
    mNetFile = new NetFileIO(fd, info->blockSize);
    //file = new FileIO();

    char filePath[256];

    splicString(filePath, sizeof(filePath), 7, QDENV::PATH, "/", info->user, "/", info->path,"/", info->name);

    mLocalFile = new FileIO(filePath, info->blockSize, info->size);

}

void FileTask::addTask(int mode){

    // 推 0,   读取本地文件, 发送到网络
    if(mode == 0){
        mLocalFile->Open("r");
        QD_LOGD("push mode");

        char buff[8192];

        int rSize = 0;

        while(1){
            rSize = mLocalFile->Read(buff, mLocalFile->getBlockSize());
            mNetFile->Write(buff, rSize);

            if(rSize <= 0){
                break;
            }

        }
    }


    // 拉 1, 读取网络文件, 写入到本地
    if(mode == 1 ){
        mLocalFile->Open("w");

        QD_LOGD("pull mdoe");

        char buff[8192];
        // 简化成普通读写操作
        while(1){
            memset(buff,0, sizeof(buff));

            // 读取 buff
            int rSize = mNetFile->Read(buff);

            if(rSize <= 0){
                break;
            }

            // 写入buff
            mLocalFile->Write(buff, rSize);

        }

    }

}


